﻿using System.IO;

namespace Coldairarrow.Util
{
    public static class PathHelper
    {
        /// <summary>
        /// 获取Url
        /// </summary>
        /// <param name="virtualUrl">虚拟Url</param>
        /// <returns></returns>
        public static string GetUrl(string virtualUrl)
        {
            //if (!virtualUrl.IsNullOrEmpty())
            //{
            //    UrlHelper urlHelper = new UrlHelper(AutofacHelper.GetScopeService<IActionContextAccessor>().ActionContext);

            //    return urlHelper.Content(virtualUrl);
            //}
            //else
            throw new System.Exception();
        }

        /// <summary>
        /// 获取绝对路径
        /// </summary>
        /// <param name="virtualPath">虚拟路径</param>
        /// <returns></returns>
        public static string GetAbsolutePath(string virtualPath)
        {
            //string path = virtualPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
            //if (path[0] == '~')
            //    path = path.Remove(0, 2);
            //string rootPath = AutofacHelper.GetScopeService<IHostingEnvironment>().WebRootPath;

            //return Path.Combine(rootPath, path);

            throw new System.Exception();
        }

        /// <summary>
        /// 获取项目代码根目录
        /// </summary>
        /// <returns></returns>
        public static string GetProjectRootpath()
        {
            //return AutofacHelper.GetScopeService<IHostingEnvironment>().ContentRootPath;
            throw new System.Exception();
        }

        public static bool DeleteFile(string file)
        {
            try
            {
                //去除文件夹和子文件的只读属性
                //去除文件夹的只读属性
                //System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
                //fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;

                //去除文件的只读属性
                //System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);

                //判断文件夹是否还存在
                

                bool isexiste = Directory.Exists(file);
                if (isexiste)
                {
                    foreach (string f in Directory.GetFileSystemEntries(file))
                    {

                        if (File.Exists(f))
                        {
                            //如果有子文件删除文件
                            File.Delete(f);
                        }
                        else
                        {
                            //循环递归删除子文件夹
                            DeleteFile(f);
                        }

                    }

                    //删除空文件夹

                    Directory.Delete(file);

                }
                return true;

            }
            catch (System.Exception) // 异常处理
            {
                return false;
            }
        }
    }
}
